package com.cal.utms.dao.payment;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.Receipt;
import com.cal.utms.util.HibernateUtil;

public class ReceiptDaoImpl implements ReceiptDao {

	@SuppressWarnings("unchecked")
	public List<Receipt> getReceipts() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		List<Receipt> receipts = null;
		try {
			tr = session.beginTransaction();
			receipts = session.createQuery("from Receipt R where R.status=0").list();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipts;
	}

	@SuppressWarnings("unchecked")
	public List<Receipt> getconfirmReceipts() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		List<Receipt> receipts = null;
		try {
			tr = session.beginTransaction();
			receipts = session.createQuery("from Receipt R where R.status=1").list();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipts;
	}

	public BigDecimal getTotalRecAmoutByCustomer(int customerCode, String customerType, int fundId) {
		Session session = HibernateUtil.getSession().openSession();
		BigDecimal totalReceipt = BigDecimal.ZERO;
		try {
			totalReceipt = (BigDecimal) session
					.createQuery(
							"select sum(R.amount) from Receipt R where R.customerType=:cusType and R.customerId=:cusId and R.fund.id=:fundId and R.status=0 GROUP BY R.customerId")
					.setParameter("cusType", customerType).setParameter("cusId", customerCode).setParameter("fundId", fundId).uniqueResult();
			if (totalReceipt == null) {
				totalReceipt = BigDecimal.ZERO;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalReceipt;
	}

	public Receipt getReceiptById(int receiptid) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		Receipt receipt = null;
		try {
			tr = session.beginTransaction();
			receipt = (Receipt) session.get(Receipt.class, receiptid);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipt;
	}

	public void confirmPayment(Receipt receipt, List<MoneyTransaction> moneyTransactions) {

		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.merge(receipt);
			for (MoneyTransaction moneyTransaction : moneyTransactions) {
				session.save(moneyTransaction);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	public Object saveReceipt(List<Receipt> receipts) {
		Object object = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		try {
			tr = session.beginTransaction();
			for (Receipt receipt : receipts) {
				object = session.save(receipt);
			}
			tr.commit();
		} catch (Exception e) {
			tr.rollback();
			e.printStackTrace();
			object = new Object();
		} finally {
			session.close();
		}
		return object;
	}

	public Receipt getReceiptByCustomer(int customerId, int fundid, Date date, String cusType) {

		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		Receipt receipt = null;
		try {
			tr = session.beginTransaction();
			receipt = (Receipt) session
					.createQuery(
							"From Receipt R where R.fund.id=:fundid and R.customerId=:custId and R.customerType=:cusType and R.paymentType=:payType and R.status=1 and R.confDate=:date")
					.setParameter("fundid", fundid).setParameter("custId", customerId).setParameter("cusType", cusType).setParameter("payType", "R").setParameter("date", date)
					.uniqueResult();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipt;
	}

	@SuppressWarnings("unchecked")
	public List<Receipt> getConfirmReceipts() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		List<Receipt> receipts = null;
		try {
			tr = session.beginTransaction();
			receipts = session.createQuery("from Receipt R where R.status=1").list();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipts;
	}

	public void rejectreceipt(Receipt receipt, List<MoneyTransaction> moneyTransactions) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.merge(receipt);
			if (moneyTransactions != null) {
				for (MoneyTransaction moneyTransaction : moneyTransactions) {
					session.save(moneyTransaction);
				}
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Receipt> getRtgsReceipt(int fundId, Date date) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tr = null;
		List<Receipt> receipts = null;
		try {
			tr = session.beginTransaction();
			receipts = session.createQuery("from Receipt R where R.status=1 and R.fund.id=:fundid and R.confDate=:date and R.paymentType=:type").setParameter("fundid", fundId)
					.setParameter("date", date).setParameter("type", "R").list();
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return receipts;
	}
}
